let is401AlertShown = false;
$(document).ajaxSend(function (e, xhr, opt) {
    let token = localStorage.getItem("user_token");
    xhr.setRequestHeader("user_header_token", token)
});

$(document).ajaxError(function(event,xhr,options,exc){
    if(xhr.status==401 && !is401AlertShown){
        alert("登录失效，请重新登录!");
        is401AlertShown = true;
        location.href = "blog_login.html";
    }else if(xhr.status==404){
        //TODO
    }else if(xhr.status==500){
        //TODO
    }
});


function getUserInfo(url){
    $.ajax({
        type: "get",
        url: url,
        success: function(result){
            let userInfo = result.data;
            console.log(userInfo)
            console.log(userInfo.picture)
            if(result!=null && result.code==200 && result.data!=null){
                $(".card h3").text(userInfo.userName);
                $(".card a").attr("href", userInfo.githubUrl);
                $(".card .sum").text(userInfo.sum);
                $(".card .count").text(userInfo.count);
                $(".card .pic").attr("src" , userInfo.picture);
            }else{
                //自行补充
                alert("作者信息获取失败！请联系管理员")
            }
        },
        error: function (result) {
            console.log(result);
            console.log(result.code);
            console.log(result.data);
        }
    });
}

function logout(){
    let logout = confirm("是否确认退出");
    if(logout){
        localStorage.removeItem("user_token");
        localStorage.removeItem("login_user_id");
        location.href = "blog_login.html";
    }
    
}